import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
// import useStore from 'vuex';
// console.log(useStore(),12312321);

// let store = useStore();
// console.log(useStore, 12312321);

// 如果要访问的地址包含在权限里面 可以访问 
// const hasUrl = (url: string, next: any) => {
//   // console.log(store.getters.getUserInfo.menus_url);
//   // 获取当前账号可以访问的路由

//   let result = store.getters.getUserInfo.menus_url;
//   // console.log(result);
//   let res = result.some((item: any) => {
//     return item == url
//   })
//   if (res) {
//     // 如果访问的地址在权限里  直接next跳转
//     next();
//     return;
//   } else {
//     // 否则强制跳转首页
//     next('/home')
//   }

// }

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    component: () => import('../pages/login.vue'),

  },
  {
    path: '/index',
    component: () => import('../pages/index.vue'),
    children: [
      {
        path: '/home',
        meta: {
          title: '首页'
        },
        component: () => import('../views/home.vue'),

      },
      {
        path: '/menu',
        meta: {
          title: '菜单管理'
        },
        component: () => import('../views/menu/menu.vue'),
       
      },
      {
        path: '/role',
        meta: {
          title: '角色管理'
        },
        component: () => import('../views/role/role.vue'),
      
      },
      {
        path: '/user',
        meta: {
          title: '管理员管理'
        },
        component: () => import('../views/user/user.vue'),
        
      },
      {
        path: '/cate',
        meta: {
          title: '商品分类'
        },
        component: () => import('../views/cate/cate.vue'),
        
      },
      {
        path: '/specs',
        meta: {
          title: '商品规格'
        },
        component: () => import('../views/specs/specs.vue'),
        
      },
      {
        path: '/goods',
        meta: {
          title: '商品管理'
        },
        component: () => import('../views/goods/goods.vue'),
        
      },
      {
        path: '',
        redirect: '/home'
      }
    ]
  },
  {
    path: '',
    redirect: '/login'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, form, next) => {
  if (to.path == '/login') {
    next();
    return;
  } else if (sessionStorage.getItem('userInfo')) {
    next();
    return;
  } else {
    next('/login');
  }


})
export default router
